export enum protocol {
  udp, tcp
}

export enum type {
  remote, local
}

export enum operation {
  create, close
}

export enum OperationType {
  create, connect, close, startGyroscope, stopGyroscope,setAddress
}


export class Exchange {
  public type: OperationType;
  public port?: number;
  public ip?: string;

  constructor(type: OperationType, ip?: string, port?: number) {
    this.type = type;
    this.port = port;
    this.ip = ip;
  }

  public static fromObject(obj: Object): Exchange {
    if (obj.hasOwnProperty("type")) {
      let exchange = new Exchange(obj['type']);
      if (obj.hasOwnProperty("port")) {
        exchange.port = obj['port'];
      }
      if (obj.hasOwnProperty("ip")) {
        exchange.ip = obj['ip'];
      }
      return exchange;
    } else {
      return null;
    }
  }

  public toObject(): Object {
    return { type: this.type, port: this.port, ip: this.ip };
  }
}


export class SocketExchange {
  public protocol: protocol;
  public port: number;
  public ip: string;
  public type: type;
  public operation: operation;

  constructor(protocol: protocol, port: number, ip: string, type: type, operation: operation) {
    this.protocol = protocol;
    this.port = port;
    this.ip = ip;
    this.type = type;
    this.operation = operation;
  }

  //如果不是该类的实例，就返回null
  public static fromObject(obj: Object): SocketExchange {
    if (obj.hasOwnProperty("protocol") && obj.hasOwnProperty("port") && obj.hasOwnProperty("ip") && obj.hasOwnProperty("type")) {
      return new SocketExchange(obj['protocol'], obj['port'], obj['ip'], obj['type'], obj['operation']);
    } else {
      return null;
    }
  }

  public toObject(): Object {
    return {
      protocol: this.protocol,
      port: this.port,
      ip: this.ip,
      type: this.type,
      operation: this.operation
    };
  }
}

export class MessageExchange {
  public message: Object;
  public port: number;
  public ip: string;

  constructor(message: Object, port: number, ip: string) {
    this.message = message;
    this.port = port;
    this.ip = ip;
  }

  //如果不是该类的实例，就返回null
  public static fromObject(obj: Object): MessageExchange {
    if (obj.hasOwnProperty("message") && obj.hasOwnProperty("port") && obj.hasOwnProperty("ip")) {
      return new MessageExchange(obj['message'], obj['port'], obj['ip']);
    } else {
      return null;
    }
  }

  public toObject(): Object {
    return { message: this.message, port: this.port, ip: this.ip };
  }
}